<template>
  <!-- 搜索专辑 -->
  <div class="sc-album">
    <search-not-found v-if="!found"></search-not-found>
    <div v-if="found">
      <ul class="alb-lists clearfix">
        <li class="alb-item" v-for="(item, index) in albums" :key="index">
          <disc :item="item"></disc>
        </li>
      </ul>
      <div class="alb-el-pagination" v-if="albumCount > 30">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="30"
          layout="prev, pager, next, jumper"
          :total="albumCount"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import Disc from "components/disc/Disc.vue";
import SearchNotFound from "views/search/child/SearchNotFound";

export default {
  components: {
    Disc,
    SearchNotFound,
  },
  data() {
    return {
      albums: [],
      albumCount: 0,
      currentPage: 1,
      found: true,
    };
  },
  mounted() {
    this.getSearchAlbum();
  },
  methods: {
    handleCurrentChange() {
      this.getSearchAlbum();
      document.documentElement.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },
    getSearchAlbum() {
      this.axios({
        url:
          "/cloudsearch?type=10&keywords=" +
          this.$store.state.MInput +
          "&offset=" +
          (this.currentPage - 1) * 30,
      })
        .then((res) => {
          if (!res.data.result || res.data.code == 400) {
            this.found = false;
          } else {
            this.albums = res.data.result.albums;
            this.albumCount = res.data.result.albumCount;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
.sc-album {
  margin-bottom: 50px;
  padding: 20px;
  overflow: hidden;
}
.sc-album .alb-lists {
  margin-right: -20px;
}
.sc-album .alb-item {
  width: 20%;
  float: left;
}
.sc-album .alb-el-pagination {
  position: relative;
  text-align: center;
  margin: 20px auto;
}
</style>